﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gagnatek.WebSite.Models;

namespace Gagnatek.WebSite.Controllers
{
    public class LoansController : Controller
    {
        private GagnatekDBEntities db = new GagnatekDBEntities();

        //
        // GET: /Loans/

        public ViewResult Index()
        {
            var loans = db.Loans.Include(l => l.Medias).Include(l => l.Users);
            return View(loans.ToList());
        }

        //
        // GET: /Loans/Details/5

        public ViewResult Details(int id)
        {
            Loans loans = db.Loans.Find(id);
            return View(loans);
        }

        //
        // GET: /Loans/Create

        public ActionResult Create(int mediaId = 1, String userName = "admin")
        {
            ViewBag.media_id = new SelectList(db.Medias, "id", "name");
            ViewBag.user_id = new SelectList(db.Users, "id", "email");

            Users user = db.Users.Where(u => u.email.Equals(userName)).FirstOrDefault();
            if (user == null) return View();

            ViewBag.mediaId = mediaId;
            ViewBag.userId = user.id;

            return View(new Loans { pickedAt = DateTime.Now, returnedAt = null, media_id = 45, user_id = user.id });
        }

        //
        // POST: /Loans/Create

        [HttpPost]
        public ActionResult Create(Loans loans, int mediaId = -1, int userId = -1)
        {
            if (mediaId != -1 && userId != -1)
            {
                loans.media_id = mediaId;
                loans.user_id = userId;
            }

            loans.pickedAt = DateTime.Now;
            loans.returnedAt = DateTime.Now;

            if (User.IsInRole("Customer"))
            {
                loans.reserved = 1;
            }
            else
            {
                loans.reserved = 0;
            }

            if (ModelState.IsValid)
            {
                db.Loans.Add(loans);
                db.SaveChanges();
                return RedirectToAction("Index", "Home");
            }

            ViewBag.media_id = new SelectList(db.Medias, "id", "name", loans.media_id);
            ViewBag.user_id = new SelectList(db.Users, "id", "email", loans.user_id);
            return View(loans);
        }

        //
        // GET: /Loans/Edit/5

        public ActionResult Edit(int id)
        {
            Loans loans = db.Loans.Find(id);
            ViewBag.media_id = new SelectList(db.Medias, "id", "name", loans.media_id);
            ViewBag.user_id = new SelectList(db.Users, "id", "email", loans.user_id);
            return View(loans);
        }

        //
        // POST: /Loans/Edit/5

        [HttpPost]
        public ActionResult Edit(Loans loans)
        {
            if (ModelState.IsValid)
            {
                db.Entry(loans).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.media_id = new SelectList(db.Medias, "id", "name", loans.media_id);
            ViewBag.user_id = new SelectList(db.Users, "id", "email", loans.user_id);
            return View(loans);
        }

        //
        // GET: /Loans/Delete/5

        public ActionResult Delete(int id)
        {
            Loans loans = db.Loans.Find(id);
            return View(loans);
        }

        //
        // POST: /Loans/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Loans loans = db.Loans.Find(id);
            db.Loans.Remove(loans);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}